regression_discontinuity <- function(
  data, var_cutoffs, est_rdd_power
) {

  # initial binding of globals
  `%>%` <- magrittr::`%>%`
  win_cabinet <- relative_date <- election <- swd <- NULL

  # split sample into winners and losers ------------------------------------
  df_winners <- dplyr::filter(.data = data, win_cabinet == 1)
  df_losers <- dplyr::filter(.data = data, win_cabinet == 0)

  # Iceland -----------------------------------------------------------------

  # winners
  tmp_winners_ice <- df_winners %>%
    # keep only Iceland
    dplyr::filter(election == "Iceland 2017 general election") %>%
    # drop observations where the running variable is 0
    dplyr::filter(relative_date  != 0)

  # losers
  tmp_losers_ice <- df_losers %>%
    # keep only Iceland
    dplyr::filter(election == "Iceland 2017 general election") %>%
    # drop observations where the running variable is 0
    dplyr::filter(relative_date  != 0)

  # out table winners
  sink("./tables/table_S_C3.txt")
  ice_winners_table <- summary(rdrobust::rdrobust( # nolint
    # the outcome
    y = dplyr::pull(.data = tmp_winners_ice, var = swd),
    # the running variable
    x = dplyr::pull(.data = tmp_winners_ice, var = relative_date),
    # the cutoff
    c = 0,
    # variable bandwidths either side of the cutoff
    bwselect = "certwo",
    # uniform kernel
    kernel = "uniform",
    # report all estimates
    all = TRUE
  ))

  # out table winners
  ice_losers_table <- summary(rdrobust::rdrobust( # nolint
    # the outcome
    y = dplyr::pull(.data = tmp_losers_ice, var = swd),
    # the running variable
    x = dplyr::pull(.data = tmp_losers_ice, var = relative_date),
    # the cutoff
    c = 0,
    # variable bandwidths either side of the cutoff
    bwselect = "certwo",
    # uniform kernel
    kernel = "uniform",
    # report all estimates
    all = TRUE
  ))
  sink()

  # Iceland Doughnut RDD Electoral Winners
  ice_winners_var_cutoff_plot <- var_cutoffs(
    data = tmp_winners_ice,
    outcome = "swd",
    running.var = "relative_date",
    filename = "./figures/RDD_Iceland_Winners.png",
    plot_title = "Iceland 2017 Electoral Winners",
    cutoffs = NULL
  )

  # Iceland Doughnut RDD Electoral Losers
  ice_losers_var_cutoff_plot <- var_cutoffs(
    data = tmp_losers_ice,
    outcome = "swd",
    running.var = "relative_date",
    filename = "./figures/RDD_Iceland_Losers.png",
    plot_title = "Iceland 2017 Electoral Losers",
    cutoffs = NULL
  )

  # power
  ice_winners_power <- est_rdd_power( # nolint
    data = tmp_winners_ice,
    outcome = "swd",
    running.var = "relative_date",
    tau = .7
  )

  ice_losers_power <- est_rdd_power( # nolint
    data = tmp_losers_ice,
    outcome = "swd",
    running.var = "relative_date",
    tau = .9
  )

  # Netherlands -------------------------------------------------------------

  # winners
  tmp_winners_nld <- df_winners %>%
    # keep only Netherlands
    dplyr::filter(election == "Netherlands 2012 general election") %>%
    # drop observations where the running variable is 0
    dplyr::filter(relative_date  != 0)

  # losers
  tmp_losers_nld <- df_losers %>%
    # keep only Netherlands
    dplyr::filter(election == "Netherlands 2012 general election") %>%
    # drop observations where the running variable is 0
    dplyr::filter(relative_date  != 0)

  # Netherlands Doughnut RDD Electoral Winners
  nld_winners_var_cutoff_plot <- var_cutoffs(
    data = tmp_winners_nld,
    outcome = "swd",
    running.var = "relative_date",
    filename = "./figures/RDD_Netherlands_Winners.png",
    plot_title = "Netherlands 2012 Electoral Winners",
    cutoffs = NULL
  )

  # out table winners
  sink("./tables/table_S_C1.txt")
  nld_winners_table <- summary(rdrobust::rdrobust( # nolint
    # the outcome
    y = dplyr::pull(.data = tmp_winners_nld, var = swd),
    # the running variable
    x = dplyr::pull(.data = tmp_winners_nld, var = relative_date),
    # the cutoff
    c = 0,
    # variable bandwidths either side of the cutoff
    bwselect = "certwo",
    # uniform kernel
    kernel = "uniform",
    # report all estimates
    all = TRUE
  ))

  # out table losers
  nld_losers_table <- summary(rdrobust::rdrobust( # nolint
    # the outcome
    y = dplyr::pull(.data = tmp_losers_nld, var = swd),
    # the running variable
    x = dplyr::pull(.data = tmp_losers_nld, var = relative_date),
    # the cutoff
    c = 0,
    # variable bandwidths either side of the cutoff
    bwselect = "certwo",
    # uniform kernel
    kernel = "uniform",
    # report all estimates
    all = TRUE
  ))
  sink()

  # Netherlands Doughnut RDD Electoral Losers
  nld_losers_var_cutoff_plot <- var_cutoffs(
    data = tmp_losers_nld,
    outcome = "swd",
    running.var = "relative_date",
    filename = "./figures/RDD_Netherlands_Losers.png",
    plot_title = "Netherlands 2012 Electoral Losers",
    cutoffs = NULL
  )

  # power
  nld_winners_power <- est_rdd_power( # nolint
    data = tmp_winners_nld,
    outcome = "swd",
    running.var = "relative_date",
    tau = .5
  )

  nld_losers_power <- est_rdd_power( # nolint
    data = tmp_losers_nld,
    outcome = "swd",
    running.var = "relative_date",
    tau = .5,
    cutoff = 0
  )

  # Norway ------------------------------------------------------------------

  # winners
  tmp_winners_nor <- df_winners %>%
    # keep only Norway
    dplyr::filter(election == "Norway 1997 general election") %>%
    # drop observations where the running variable is 0
    dplyr::filter(relative_date  != 0)

  # losers
  tmp_losers_nor <- df_losers %>%
    # keep only Norway
    dplyr::filter(election == "Norway 1997 general election") %>%
    # drop observations where the running variable is 0
    dplyr::filter(relative_date  != 0)

  # out table winners
  nor_winners_table <- summary(rdrobust::rdrobust( # nolint
    # the outcome
    y = dplyr::pull(.data = tmp_winners_nor, var = swd),
    # the running variable
    x = dplyr::pull(.data = tmp_winners_nor, var = relative_date),
    # the cutoff
    c = 0,
    # variable bandwidths either side of the cutoff
    bwselect = "certwo",
    # uniform kernel
    kernel = "uniform",
    # report all estimates
    all = TRUE
  ))

  # out table losers
  nor_losers_table <- summary(rdrobust::rdrobust( # nolint
    # the outcome
    y = dplyr::pull(.data = tmp_losers_nor, var = swd),
    # the running variable
    x = dplyr::pull(.data = tmp_losers_nor, var = relative_date),
    # the cutoff
    c = 0,
    # variable bandwidths either side of the cutoff
    bwselect = "certwo",
    # uniform kernel
    kernel = "uniform",
    # report all estimates
    all = TRUE
  ))

  # Norway Doughnut RDD Electoral Winners
  nor_winners_var_cutoff_plot <- var_cutoffs(
    data = tmp_winners_nor,
    outcome = "swd",
    running.var = "relative_date",
    filename = "./figures/RDD_Norway_Winners.png",
    plot_title = "Norway 1997 Electoral Winners",
    cutoffs = NULL
  )

  # Norway Doughnut RDD Electoral Losers
  nor_losers_var_cutoff_plot <- var_cutoffs(
    data = tmp_losers_nor,
    outcome = "swd",
    running.var = "relative_date",
    filename = "./figures/RDD_Norway_Losers.png",
    plot_title = "Norway 1997 Electoral Losers",
    cutoffs = NULL
  )

  # power
  nor_winners_power <- est_rdd_power( # nolint
    data = tmp_winners_nor,
    outcome = "swd",
    running.var = "relative_date",
    tau = .7
  )

  nor_losers_power <- est_rdd_power( # nolint
    data = tmp_losers_nor,
    outcome = "swd",
    running.var = "relative_date",
    tau = .7
  )


  # Iceland & Netherlands pooled --------------------------------------------

  # winners
  tmp_winners_ice_nld <- df_winners %>%
    # drop Norway
    dplyr::filter(election != "Norway 1997 general election") %>%
    # drop observations where the running variable is 0
    dplyr::filter(relative_date  != 0)

  # losers
  tmp_losers_ice_nld <- df_losers %>%
    # drop Norway
    dplyr::filter(election != "Norway 1997 general election") %>%
    # drop observations where the running variable is 0
    dplyr::filter(relative_date  != 0)

  # Iceland & Netherlands Doughnut RDD Electoral Winners
  var_cutoffs(
    title.offset = 1,
    subtitle.offset = -.4,
    data = tmp_winners_ice_nld,
    outcome = "swd",
    running.var = "relative_date",
    clustervar = "election",
    filename = "./figures/RDD_ice_and_nld_Winners.png",
    plot_title = "Iceland & Netherlands pooled Electoral Winners",
    caption = paste0(
      "Note: Robust RDD estimates with 95% confidence ",
      "intervals \n and election fixed effects included"
    ),
    cutoffs = NULL
  )

  # Iceland & Netherlands Doughnut RDD Electoral Losers
  var_cutoffs(
    title.offset = 1.7,
    subtitle.offset = -.4,
    data = tmp_losers_ice_nld,
    outcome = "swd",
    running.var = "relative_date",
    clustervar = "election",
    filename = "./figures/RDD_ice_and_nld_Losers.png",
    plot_title = "Iceland & Netherlands pooled Electoral Losers",
    caption = paste0(
      "Note: Robust RDD estimates with 95% confidence intervals ",
      "\n and election fixed effects included"
    ),
    cutoffs = NULL
  )

  # power
  ice_nld_winners_power <- est_rdd_power( # nolint
    data = tmp_winners_ice_nld,
    outcome = "swd",
    running.var = "relative_date",
    cluster = "election",
    cutoff = 0,
    tau = 0.5
  )

  ice_nld_power <- est_rdd_power( # nolint
    data = tmp_losers_ice_nld,
    outcome = "swd",
    running.var = "relative_date",
    cluster = "election",
    cutoff = 0,
    tau = 0.5
  )

  # All elections pooled ----------------------------------------------------

  # winners
  tmp_winners_all <- df_winners %>%
    # drop observations where the running variable is 0
    dplyr::filter(relative_date  != 0)

  # losers
  tmp_losers_all <- df_losers %>%
    # drop observations where the running variable is 0
    dplyr::filter(relative_date  != 0)

  # All elections Doughnut RDD Electoral Winners
  all_winners_var_cutoff_plot <- var_cutoffs(
    data = tmp_winners_all,
    outcome = "swd",
    running.var = "relative_date",
    clustervar = "election",
    filename = "./figures/RDD_all_Winners.png",
    plot_title = "Electoral Winners",
    cutoffs = NULL
  )

  # All elections Doughnut RDD Electoral Losers
  all_losers_var_cutoff_plot <- var_cutoffs(
    data = tmp_losers_all,
    outcome = "swd",
    running.var = "relative_date",
    clustervar = "election",
    filename = "./figures/RDD_all_Losers.png",
    plot_title = "Electoral Losers",
    cutoffs = NULL
  )

  # power
  all_winners_power <- est_rdd_power( # nolint
    data = tmp_winners_all,
    outcome = "swd",
    running.var = "relative_date",
    cluster = "election",
    cutoff = 0,
    tau = 0.5
  )

  all_losers_power <- est_rdd_power( # nolint
    data = tmp_losers_all,
    outcome = "swd",
    running.var = "relative_date",
    cluster = "election",
    cutoff = 0,
    tau = 0.5
  )

  # RDD results
  m_winners <- rdd_model(
    data = tmp_winners_all,
    outcome = "swd",
    clustervar = "election",
    running.var = "relative_date"
  )
  m_losers <- rdd_model(
    data = tmp_losers_all,
    outcome = "swd",
    clustervar = "election",
    running.var = "relative_date"
  )

  # results table to print
  out <- tibble::tibble(
    method = c(
      "Conventional", "Bias-Corrected", "Robust", "N",
      "BW est. (std.)", "BW est. (robust)"
    ),
    `coefficient_winners` = c(
      round(m_winners$coef[, 1], 2),
      round(m_winners$N[2], 2),
      paste0(round(m_winners$bws[1, ], digits = 0), collapse = "/"),
      paste0(round(m_winners$bws[2, ], digits = 0), collapse = "/")
    ),
    `lower_bound_winners` = c(round(m_winners$ci[, 1], 2), NA, NA, NA),
    `upper_bound_winners` = c(round(m_winners$ci[, 2], 2), NA, NA, NA),
    `coefficient_losers` = c(
      round(m_losers$coef[, 1], 2),
      round(m_losers$N[2], 2),
      paste0(round(m_losers$bws[1, ], digits = 0), collapse = "/"),
      paste0(round(m_losers$bws[2, ], digits = 0), collapse = "/")
    ),
    `lower_bound_losers` = c(round(m_losers$ci[, 1], 2), NA, NA, NA),
    `upper_bound_losers` = c(round(m_losers$ci[, 2], 2), NA, NA, NA)
  )

  # save results
  write.csv(
    x = out,
    file = "./tables/table_1.csv",
    row.names = FALSE
  )

  # plots -------------------------------------------------------------------

  # pooled elections
  p <- ggpubr::ggarrange( # nolint
    widths = c(1, 1),
    all_winners_var_cutoff_plot, all_losers_var_cutoff_plot,
    ncol = 2, nrow = 1
  )

  ggplot2::ggsave(
    filename = "./figures/figure5.png",
    device = "png",
    width = 27,
    height = 10,
    units = "cm",
    dpi = 600
  )

  # individual results
  p <- ggpubr::ggarrange(
    widths = c(1, 1),
    ice_winners_var_cutoff_plot, ice_losers_var_cutoff_plot,
    nld_winners_var_cutoff_plot, nld_losers_var_cutoff_plot,
    nor_winners_var_cutoff_plot, nor_losers_var_cutoff_plot,
    ncol = 2, nrow = 3
  )

  ggplot2::ggsave(
    filename = "./figures/figure_S_D1.png",
    device = "png",
    width = 27,
    height = 22,
    units = "cm",
    dpi = 600
  )
}
